﻿using Fibot.Module.WangDianTong.basic;
using Fibot.Module.WangDianTong.basic.dto;
using Fibot.Module.WangDianTong.Qimen.QimenCloud.Request;
using Fibot.Module.WangDianTong.Qimen.QimenCloud.Response;
using Fibot.Module.WangDianTong.Qimen.QimenCloud;
using Fibot.Module.WangDianTong.Qimen;
using Fibot.Module.WangDianTong.trade;
using Fibot.Module.WangDianTong.trade.dto;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Fibot.Module.WangDianTong;

public class WdtManager
{
    /// <summary>
    /// 旺店通配置项
    /// </summary>
    public WdtOptions options;

    /// <summary>
    /// 构造器
    /// </summary>
    /// <param name="_options"></param>
    public WdtManager(WdtOptions _options)
    {
        options = _options;
    }

    #region 公共方法

    /// <summary>
    /// 查询店铺
    /// </summary>
    /// <returns></returns>
    public List<ShopGetOutputDto> queryShop()
    {
        ShopService service = new ShopService(options);
        ShopGetListInputVo vo = new ShopGetListInputVo()
        {
            shop_no = "",
            page_no = 0,
            page_size = 20
        };
        var result = service.query(vo);
        if (result?.code == 0)
        {
            return result.shoplist;
        }
        throw new Exception(result?.message);
    }

    /// <summary>
    /// 查询时间范围内的所有出库单
    /// </summary>
    /// <param name="startTime"></param>
    /// <param name="endTime"></param>
    /// <returns></returns>
    public List<StockoutOrderTradeGetOutputDto> QueryStockoutOrderTradeService(DateTime startTime, DateTime endTime)
    {
        List<StockoutOrderTradeGetOutputDto> list = new List<StockoutOrderTradeGetOutputDto>();
        int pageNo = 0;
        int pageSize = 100;
        int returnRow = 100;
        while (returnRow > 0)
        {
            var rList = QueryStockoutOrderTradeService_wdt(startTime, endTime, pageNo, pageSize, 95);
            list = list.Concat(rList).ToList();
            pageNo++;
            returnRow = rList.Count;
        }

        pageNo = 0;
        pageSize = 100;
        returnRow = 100;
        while (returnRow > 0)
        {
            var rList = QueryStockoutOrderTradeService_wdt(startTime, endTime, pageNo, pageSize, 110);
            list = list.Concat(rList).ToList();
            pageNo++;
            returnRow = rList.Count;
        }

        pageNo = 0;
        pageSize = 100;
        returnRow = 100;
        while (returnRow > 0)
        {
            var rList = QueryStockoutOrderTradeService_qimen(startTime, endTime, pageNo, pageSize, 0);
            list = list.Concat(rList).ToList();
            pageNo++;
            returnRow = rList.Count;
        }
        return list;
    }

    #endregion

    #region 旺店通标准SDK

    /// <summary>
    /// 查询淘系以外销售出库（抖音，快手）
    /// </summary>
    /// <param name="startTime"></param>
    /// <param name="endTime"></param>
    /// <param name="pageNo"></param>
    /// <param name="pageSize"></param>
    /// <param name="status"></param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>
    public List<StockoutOrderTradeGetOutputDto> QueryStockoutOrderTradeService_wdt(DateTime startTime, DateTime endTime, int pageNo, int pageSize, int status)
    {
        StockoutOrderTradeService service = new StockoutOrderTradeService(options);

        StockoutOrderTradeGetListInputVo vo = new StockoutOrderTradeGetListInputVo()
        {
            start_time = startTime,//new DateTime(2023,12,18,12,0,0),
            end_time = endTime,//new DateTime(2023,12,18,20,0,0,0),
            page_no = pageNo,
            page_size = pageSize,
            status = status
        };
        var result = service.query(vo);
        if (result?.code == 0)
        {
            return result.stockout_list;
        }
        throw new Exception(result?.message);

    }

    #endregion

    #region 奇门

    /// <summary>
    /// 查询淘系销售出库
    /// </summary>
    /// <param name="startTime"></param>
    /// <param name="endTime"></param>
    /// <param name="pageNo"></param>
    /// <param name="pageSize"></param>
    /// <param name="status"></param>
    /// <returns></returns>
    public List<StockoutOrderTradeGetOutputDto> QueryStockoutOrderTradeService_qimen(DateTime startTime, DateTime endTime, int pageNo, int pageSize, int status)
    {
        string serverUrl = options.ServerUrl;
        string appKey = options.AppkeyQimen;//自己申请的奇门appkey
        string appSecret = options.AppSecretQimen;//自己申请的奇门appkey对应的secret
        string format = options.format;

        DefaultQimenCloudClient client = new DefaultQimenCloudClient(serverUrl, appKey, appSecret, format);
        WdtStockoutOrderQueryTradeRequest req = new WdtStockoutOrderQueryTradeRequest();
        req.SetTargetAppKey(options.TargetAppKey);//旺店通的奇门appkey
        req.Sid = options.Sid;//旺店通erp的卖家账号
        req.StartTime = startTime.ToString("yyyy-MM-dd HH:mm:ss");
        req.EndTime = endTime.ToString("yyyy-MM-dd HH:mm:ss");
        req.PageSize = pageSize;
        req.PageNo = pageNo;
        req.Status = status;

        WdtStockoutOrderQueryTradeResponse res = client.Execute(req);
        JObject? bodyObj = JsonConvert.DeserializeObject<JObject>(res.Body);       
        StockoutOrderTradeGetListOutputDto? result = JsonConvert.DeserializeObject<StockoutOrderTradeGetListOutputDto>(bodyObj["response"].ToString());
        if (result?.code == 0)
        {
            return result.stockout_list;
        }

        return new List<StockoutOrderTradeGetOutputDto>();
    }

    #endregion

}
